﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ChannelVN.Cached;
using ChannelVN.Cached.CacheClients;

namespace GUI
{
    public partial class memcachedmgr : System.Web.UI.Page
    {
        private const string SessionName = "MemcachedMgrSession";
        private Memcached CachedInstance
        {
            get
            {
                Memcached memcached = null;
                memcached = ddlMemcachedServer.SelectedValue == "MemcachedForData"
                                ? CacheController.GetMemcachedInstanceForDataCached()
                                : CacheController.GetMemcachedInstance(ddlMemcachedServer.SelectedValue);
                return memcached;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[SessionName] != null && Session[SessionName].ToString() == DateTime.Now.ToString("ddMMyyyy"))
            {
                mvPanel.SetActiveView(vCacheManager);
            }
            else
            {
                mvPanel.SetActiveView(vLogin);
            }
        }

        protected void btnGetCache_Click(object sender, EventArgs e)
        {
            try
            {
                if (Session[SessionName] == null || Session[SessionName].ToString() != DateTime.Now.ToString("ddMMyyyy")) return;

                var group = "";
                if (ddlKeyGroup.SelectedValue != "" && ddlKeyGroup.SelectedValue != "default")
                {
                    group = ddlKeyGroup.SelectedValue + txtGroupName.Text;
                }
                else
                {
                    group = ddlKeyGroup.SelectedValue;
                }
                var value = CachedInstance.Get(group, txtCachedKey.Text);
                if (value is DataTable || value is DataSet || value is IList)
                {
                    lblCachedValue.Visible = false;
                    grvCachedValue.Visible = true;
                    grvCachedValue.DataSource = value;
                    grvCachedValue.DataBind();
                }
                else
                {
                    lblCachedValue.Visible = true;
                    grvCachedValue.Visible = false;
                    lblCachedValue.Text = (value != null ? value.ToString() : "");
                }
            }
            catch (Exception ex)
            {
                cusvError.ErrorMessage = ex.ToString();
                cusvError.IsValid = false;
                throw;
            }
        }

        protected void btnRemoveCached_Click(object sender, EventArgs e)
        {
            try
            {
                if (Session[SessionName] == null || Session[SessionName].ToString() != DateTime.Now.ToString("ddMMyyyy")) return;

                var group = "";
                if (ddlKeyGroup.SelectedValue != "" && ddlKeyGroup.SelectedValue != "default")
                {
                    group = ddlKeyGroup.SelectedValue + txtGroupName.Text;
                }
                else
                {
                    group = ddlKeyGroup.SelectedValue;
                }
                CachedInstance.Remove(group, txtCachedKey.Text);

                lblCachedValue.Visible = true;
                grvCachedValue.Visible = false;
                lblCachedValue.Text = "Cached removed!";
            }
            catch (Exception ex)
            {
                cusvError.ErrorMessage = ex.ToString();
                cusvError.IsValid = false;
                throw;
            }
        }

        protected void btnRemoveAllGroup_Click(object sender, EventArgs e)
        {
            try
            {
                if (Session[SessionName] == null || Session[SessionName].ToString() != DateTime.Now.ToString("ddMMyyyy")) return;

                var group = "";
                if (ddlKeyGroup.SelectedValue != "" && ddlKeyGroup.SelectedValue != "default")
                {
                    group = ddlKeyGroup.SelectedValue + txtGroupName.Text;
                }
                else
                {
                    group = ddlKeyGroup.SelectedValue;
                }
                //CachedInstance.RemoveAll(group);
                CacheController.RemoveAllMemcachedGroup(group);

                lblCachedValue.Visible = true;
                grvCachedValue.Visible = false;
                lblCachedValue.Text = "All Cached Group removed!";
            }
            catch (Exception ex)
            {
                cusvError.ErrorMessage = ex.ToString();
                cusvError.IsValid = false;
                throw;
            }
        }

        protected void btnRemoveAllServer_Click(object sender, EventArgs e)
        {
            try
            {
                if (Session[SessionName] == null || Session[SessionName].ToString() != DateTime.Now.ToString("ddMMyyyy")) return;

                CachedInstance.RemoveAllPort();

                lblCachedValue.Visible = true;
                grvCachedValue.Visible = false;
                lblCachedValue.Text = "All Cached removed!";
            }
            catch (Exception ex)
            {
                cusvError.ErrorMessage = ex.ToString();
                cusvError.IsValid = false;
                throw;
            }
        }

        protected void btnGetAllCachedKey_Click(object sender, EventArgs e)
        {
            try
            {
                if (Session[SessionName] == null || Session[SessionName].ToString() != DateTime.Now.ToString("ddMMyyyy")) return;

                lblCachedValue.Visible = true;
                lblCachedValue.Text = SimpleMemcachedKeyGroup.Current.ToString();
                grvCachedValue.Visible = false;
            }
            catch (Exception ex)
            {
                cusvError.ErrorMessage = ex.ToString();
                cusvError.IsValid = false;
                throw;
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtPassword.Text == "Mem123")
            {
                Session[SessionName] = DateTime.Now.ToString("ddMMyyyy");
                mvPanel.SetActiveView(vCacheManager);
            }
        }

        protected void btnLogout_Click(object sender, EventArgs e)
        {
            Session[SessionName] = "";
            mvPanel.SetActiveView(vLogin);
        }
    }
}